package com.mspbots.core.wise.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.mspbots.core.DefaultBaseMapper;
import com.mspbots.core.wise.controller.params.UserMappingParams;
import com.mspbots.core.wise.model.TenantUserMapping;
import com.mspbots.core.wise.model.dto.UserMappingCommand;
import com.mspbots.core.wise.model.dto.UserMappingDetail;
import com.mspbots.core.wise.model.dto.UserMappingResponse;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * @author billb
 */
@Mapper
public interface TenantUsersMappingMapper extends DefaultBaseMapper<TenantUserMapping> {

    /**
     * find one mapping entity
     *
     * @param queryWrapper
     * @return TenantUserMapping return mapping
     */
    @Select(value = "SELECT mapping.*,tu.cw_uid,tu.firstname AS firstName, tt.teams_mail as teamsEmail,tu.identifier," +
            "CONCAT(tu.firstname,' ',tu.lastname) AS userName," +
            "CONCAT(ts.first_name,' ',ts.last_name) AS tsheetUserName,tt.teams_display_name AS teamsUserName " +
            "FROM tenant_user_mapping AS mapping " +
            "LEFT JOIN tenant_user tu ON tu.id=mapping.tenant_user_id " +
            "LEFT JOIN tsheet_users ts ON ts.id=mapping.sheet_user_id " +
            "LEFT JOIN tenant_teams_user tt ON tt.teams_user_id=mapping.teams_user_id " +
            " ${ew.customSqlSegment} ORDER BY mapping.id DESC LIMIT 1")
    UserMappingResponse findByOne(@Param(Constants.WRAPPER) Wrapper<TenantUserMapping> queryWrapper);

    UserMappingDetail detail(UserMappingParams params);

    UserMappingCommand selectCommandDetail(UserMappingParams params);

    UserMappingCommand selectCommandDetailTenantConf(Long tenantId);


}
